#include "PlayerController.h"
#include "InputManager.h"
#include "texturelibrary.h"
#include "GameStateManager.h"
#include "GameStateEnum.h"

#include <iostream>

void PlayerController::Update(float deltaTime)
{
	energy -= deltaTime * 1.0f;
	float powerBarScale = energy / energyMax;
	powerBarFront.setScale(sf::Vector2f(powerBarScale, 1.0f));


	//apply gravity
	acc.y = 200.0f;

	scale;

	//integrate over y
	//dv = a * dt  - delta velocity = acceleration * delta time
	float dvY = acc.y * deltaTime;
	vel.y += dvY;
	//dp = v * dt
	float dpY = vel.y * deltaTime;
	pos.y += dpY;

	//Move Towards my horizontal position
	float moveToX = minXpos + ((maxXPos - minXpos)*(energy / energyMax));
	pos.x = Lerp(pos.x, moveToX, 0.5f);

	if (InputManager::UpArrowPressed() && !isDead)
	{
		vel.y -= 200.0f;
	}
	else if (isDead)
	{
		justdiedTimer += deltaTime;
		if (justdiedTimer >= 1.0f)
		{
			GameStateManager::SetGameState(PlayingDied);
		}
	}

}

void PlayerController :: AteFish(float fishSize)
{
	energy += abs(fishSize * 5.0f);
	scale += abs(0.1* fishSize);
	mySprite.setScale(sf::Vector2f(scale, scale));
	Clamp(energy, 0.0f, energyMax);
	float powerBarScale = energy / energyMax;
	powerBarFront.setScale(sf::Vector2f(powerBarScale, 1.0f));
}

void PlayerController :: MakePowerBarSprites()
{
	sf::Texture *myTexture = NULL;
	myTexture = TT::TextureLibrary["images/PowerBarBackImage.png"];
	// Make a sprite and set its origin:
	if (myTexture != NULL)
	{
		powerBarBack = sf::Sprite(*myTexture);
		powerBarBack.setOrigin(0.0f, myTexture->getSize().y * 0.5f);
		powerBarBack.setPosition(sf::Vector2f(512.0f, 50.0f));
	}
	else
	{
		cout << "ERROR: Objects texture does not exist!!" << endl;
	}

	myTexture = NULL;
	myTexture = TT::TextureLibrary["images/PowerBarFrontImage.png"];
	// Make a sprite and set its origin:
	if (myTexture != NULL)
	{
		powerBarFront = sf::Sprite(*myTexture);
		powerBarFront.setOrigin(0.0f, myTexture->getSize().y * 0.5f);
		powerBarFront.setPosition(sf::Vector2f(512.0f, 50.0f));
		float powerBarScale = energy / energyMax;
		powerBarFront.setScale(sf::Vector2f(powerBarScale, 1.0f));
	}
	else
	{
		cout << "ERROR: Objects texture does not exist!!" << endl;
	}

}

void PlayerController::Die()
{
	sf::Texture *myNewTexture = NULL;
	myNewTexture = TT::TextureLibrary[skeletonFileName];
	if (myNewTexture != NULL)
	{
		isScrolingObject = true;
		isPhysical = false;
		isDead = true;
		mySprite.setTexture(*myNewTexture);
	}
	else
		std::cout << "ERROR: could not load sprite with Skeleton Image" << std::endl;
}